﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace LibExtend
{
    public partial class Menu : UserControl
    {
        public event EventHandler RowDoubleClicked;
        public event EventHandler ItemClicked;
        GridHitInfo downHitInfo = null;

        DataTable iItem = new DataTable();
        private int old_Index = -1;
        private int ClickedIndex = -1;

        public DataTable Items 
        {
            get { return iItem; }
            set { iItem = value; Invalidate(); }
        }

        [DefaultValue(false)]
        public bool ShowFilterRow
        {
            get { return _view.OptionsView.ShowAutoFilterRow; }
            set { _view.OptionsView.ShowAutoFilterRow = value; }
        }

        public bool GridBorder 
        {
            set
            {
                if (value == false)
                    _view.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
                else
                    _view.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
            }
            get
            {
                if (_view.BorderStyle == DevExpress.XtraEditors.Controls.BorderStyles.Default) return true;
                else return false;
            }
        }

        public Menu()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.UserPaint |
            ControlStyles.AllPaintingInWmPaint |
            ControlStyles.ResizeRedraw, true);

            InitializeComponent();

            if (ShowFilterRow)
                _view.OptionsView.ShowAutoFilterRow = true;
        }

        private void Menu_Paint(object sender, PaintEventArgs e)
        {
            if (Items == null || Items.Rows.Count <= 0) return;
            for(int i=0;i<Items.Rows.Count;i++)
            {
                 e.Graphics.DrawImage(GetImage(Items.Rows[i]["HinhAnh"]), 8, (i) * 48, 48, 48);
            }
        }

        private void Menu_MouseMove(object sender, MouseEventArgs e)
        {
            int iCount = 0;

            if (Items != null && Items.Rows.Count > 0)
                iCount = Items.Rows.Count;

            if (e.X >= 5 && e.X <= 48 + 5)
            {
                if (e.Y > 0 && e.Y <= 48 * iCount)
                {
                    int index = e.Y / 48;
                    if (index == old_Index) return;

                    if (index >= iCount) return;
                    ResetBtn(index);

                    old_Index = index;
                }
                //else if (ClickedIndex >= 0)
                //    ResetBtn(ClickedIndex);
            } 
            //else if (ClickedIndex >= 0)
            //    ResetBtn(ClickedIndex);

        }

        private Image GetImage(object obj)
        {
            Image image = LibExtend.Properties.Resources.NoImage;

            if (!object.Equals(obj, null) && !object.Equals(obj, DBNull.Value) && !object.Equals(obj, new byte[0]))
            {
                MemoryStream ms = new MemoryStream((byte[])obj);
                image = Image.FromStream(ms);
            }

            return image;
        }

        public void SetDetail(DataTable Data, string Cols, string Caps)
        {
            grid.DataSource = Data;

            foreach (DevExpress.XtraGrid.Columns.GridColumn col in _view.Columns)
            {
                if (col.FieldName.ToLower() != "tenhang" && col.FieldName.ToLower() != "hinhanh")
                {
                    col.Visible = false;
                }
            }

            _view.Columns.ColumnByFieldName("HinhAnh").Width = 48;

            _view.Columns.ColumnByFieldName("HinhAnh").OptionsColumn.AllowEdit = false;
            DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit pic = new DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit();
            pic.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
            
            pic.NullText = "";
            _view.Columns.ColumnByFieldName("HinhAnh").ColumnEdit = pic;
            _view.OptionsBehavior.Editable = false;
        }

        private void ResetBtn(int _index)
        {
            btn.Image = null;
            btn.Left = 4;
            btn.Top = _index * 48;

            btn.Image = GetImage(Items.Rows[_index]["HinhAnh"]);
            btn.ToolTip = Items.Rows[_index]["TenNhom"].ToString();
            btn.Visible = true;
        }

        private void btn_Click(object sender, EventArgs e)
        {
            ClickedIndex = old_Index;
            if (ItemClicked != null)
            {
                if (Items == null || Items.Rows.Count <= 0)
                    ItemClicked(null, e);
                else
                {
                    if (Items.Rows.Count > old_Index && old_Index >= 0)
                        ItemClicked(Items.Rows[old_Index], e);
                }
            }
        }

        private void _view_MouseDown(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;

            downHitInfo = null;

            GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));

            if (Control.ModifierKeys != Keys.None) return;

            if (e.Button == MouseButtons.Left && hitInfo.RowHandle >= 0)

                downHitInfo = hitInfo;
        }

        private void _view_MouseMove(object sender, MouseEventArgs e)
        {
            GridView view = sender as GridView;

            if (e.Button == MouseButtons.Left && downHitInfo != null)
            {
                Size dragSize = SystemInformation.DragSize;

                Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2,
                    downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);

                if (!dragRect.Contains(new Point(e.X, e.Y)))
                {
                    DataRow row = view.GetDataRow(downHitInfo.RowHandle);
                    view.GridControl.DoDragDrop(row, DragDropEffects.Move);
                    downHitInfo = null;
                    DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true;
                }
            }
        }

        private void _view_DoubleClick(object sender, EventArgs e)
        {
            DataRow row = _view.GetFocusedDataRow();
            if (RowDoubleClicked != null)
                RowDoubleClicked(row, e);
        }
    }
}
